<?php

class HomeController extends BaseController {

	/*
	|--------------------------------------------------------------------------
	| Default Home Controller
	|--------------------------------------------------------------------------
	|
	| You may wish to use controllers instead of, or in addition to, Closure
	| based routes. That's great! Here is an example controller method to
	| get you started. To route to this controller, just add the route:
	|
	|	Route::get('/', 'HomeController@showWelcome');
	|
	*/

	public function showHome($lang) {			
		$catList = Category::status(Options::STATUS_ACTIVE)->language()->orderBySequence("asc")->get(array(Options::DB_TABLE_CATEGORIES.'.id',Options::DB_TABLE_CATEGORIES.'.name'));		
		$mostViewed = News::status(Options::STATUS_ACTIVE)->orderByMostViewed('desc')->language()->take(Options::PAGE_HOME_MOST_VIEWED_LIMIT)->get(array(Options::DB_TABLE_NEWS.'.id', Options::DB_TABLE_NEWS.'.title', Options::DB_TABLE_CATEGORIES.'.name'));	    
		$mostCommented = News::status(Options::STATUS_ACTIVE)->orderByMostCommented('desc')->language()->take(Options::PAGE_HOME_MOST_COMMENTED_LIMIT)->get(array(Options::DB_TABLE_NEWS.'.id', Options::DB_TABLE_NEWS.'.title', Options::DB_TABLE_CATEGORIES.'.name'));
		$latestNews = News::status(Options::STATUS_ACTIVE)->orderByCreated('desc')->language()->take(Options::PAGE_HOME_LATEST_NEWS_LIMIT)->get(array(Options::DB_TABLE_NEWS.'.id', Options::DB_TABLE_NEWS.'.title', Options::DB_TABLE_NEWS.'.descriptions',Options::DB_TABLE_NEWS.'.featurePhoto', Options::DB_TABLE_CATEGORIES.'.name'));
		foreach ($catList as $value )
			$catNews[$value->name] = News::status(Options::STATUS_ACTIVE)->orderByCreated('desc')->language()->take(6)
								->categoryID($value->id)->get(array(Options::DB_TABLE_NEWS.'.id', Options::DB_TABLE_NEWS.'.title', Options::DB_TABLE_NEWS.'.descriptions',Options::DB_TABLE_NEWS.'.featurePhoto', Options::DB_TABLE_CATEGORIES.'.name'));
		$headerMenu = 'home';
		//data of slider
		$topStory = News::status(Options::STATUS_ACTIVE)->topStory('1')->language()->take(5)->get(array(Options::DB_TABLE_NEWS.'.id', Options::DB_TABLE_NEWS.'.title', Options::DB_TABLE_NEWS.'.descriptions',Options::DB_TABLE_NEWS.'.featurePhoto'));		
		//dd(DB::getQueryLog());
		return View::make('home', array(
			'headerMenu' => $headerMenu, 
			'catList' => $catList, 
			'mostViewed' => $mostViewed, 
			'mostCommented' => $mostCommented, 
			'latestNews' => $latestNews, 
			'catNews' => $catNews,
			'topStory'=>$topStory,
			
		));
	}
	
	public function listCat($lg,$id,$name){
		
		$catList = Category::status(Options::STATUS_ACTIVE)->language()->orderBySequence("asc")->get(array(Options::DB_TABLE_CATEGORIES.'.id',Options::DB_TABLE_CATEGORIES.'.name'));	
			    
		$mostCommented = News::status(Options::STATUS_ACTIVE)->orderByMostCommented('desc')->language()->get(array(Options::DB_TABLE_NEWS.'.id'));
		$latestNews = News::status(Options::STATUS_ACTIVE)->orderByCreated('desc')->language()->take(Options::PAGE_HOME_LATEST_NEWS_LIMIT)->get(array(Options::DB_TABLE_NEWS.'.id', Options::DB_TABLE_NEWS.'.title', Options::DB_TABLE_NEWS.'.descriptions',Options::DB_TABLE_NEWS.'.featurePhoto', Options::DB_TABLE_CATEGORIES.'.name'));
		foreach ($catList as $value )
			$catNews[$value->name] = News::status(Options::STATUS_ACTIVE)->orderByCreated('desc')->language()->take(Options::PAGE_HOME_CATEGORY_LIMIT)
								->categoryID($value->id)->get(array(Options::DB_TABLE_NEWS.'.id', Options::DB_TABLE_NEWS.'.title', Options::DB_TABLE_NEWS.'.descriptions',Options::DB_TABLE_NEWS.'.featurePhoto', Options::DB_TABLE_CATEGORIES.'.name'));
		$headerMenu = $name;	
		//latest categry news LatestCaNew
		$latestCatNews=News::status(Options::STATUS_ACTIVE)->latestCaNew($id)->language()->take(5)->get(array(Options::DB_TABLE_NEWS.'.id', Options::DB_TABLE_NEWS.'.title', Options::DB_TABLE_NEWS.'.descriptions',Options::DB_TABLE_NEWS.'.featurePhoto',Options::DB_TABLE_NEWS.'.created_at'));		
		$numberItem = count($mostCommented);
		
		return View::make('listCat', array(
			'headerMenu' => $headerMenu, 
			'catList' => $catList,
			'latestCatNews'=>$latestCatNews,
			'numberItem'=>$numberItem,
		));
		
	}
	
	public function showRSS() {
		$topNews = News::status(Options::STATUS_ACTIVE)->orderByCreated('desc')->language()->take(Options::PAGE_HOME_TOP_NEWS_RSS_LIMIT)->isTopNews(true)
				->get(array(Options::DB_TABLE_NEWS.'.id',Options::DB_TABLE_NEWS.'.title', Options::DB_TABLE_NEWS.'.descriptions', Options::DB_TABLE_CATEGORIES.'.name' ));
		
		return View::make( 'feed', array('topNews' => $topNews) );
	}
    public function lisNewOnCat(){  
    	$page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);  	
		    if(!is_numeric($page_number)){
			header('HTTP/1.1 500 Invalid page number!');
			exit();
		}
		
		//echo 'page_number: '.$page_number;
		
		$position = ($page_number * 6);
		//echo "Vi tri : ".$position;
		$mossLisst = News::mostViewedAll('desc')->language()->offset($position)->take(6)->get(array(Options::DB_TABLE_NEWS.'.id', Options::DB_TABLE_NEWS.'.title', Options::DB_TABLE_NEWS.'.descriptions',Options::DB_TABLE_NEWS.'.featurePhoto',Options::DB_TABLE_NEWS.'.created_at'));    	    	
    	$number = count($mossLisst);    	
    	$display="";    	  	
    	$m=1;
    	for ($i=0; $i<$number; $i=$i+2){
	    		if($m%3==0){ 
	    			$class = " divider-none";	    			
	    		}else{
	    			$class=" divider";
	    		}    		
	    		if( !empty($mossLisst[$i]->featurePhoto) ) {     			 			
	    		$display=$display.'
	    		
	    		<div class="col-md-4 col-lg-4'.$class.' clear-padding">
	    			<div class="col-sm-12 col-md-3 col-lg-3 clear-padding" style="padding-left:15px; padding-top:20px;">
	    				 <a href="'.SEO::generateNewsURL($mossLisst[$i]->id,$mossLisst[$i]->title) .'"><img src="../'.$mossLisst[$i]->featurePhoto.'" class="img-responsive" width="82" height="82"></a>
	    			 </div>
					<div class="col-sm-12 col-md-9 col-lg-9 clear-padding" style="padding-left:15px;padding-right:15px;padding-top:20px;">
						 <a href="'.SEO::generateNewsURL($mossLisst[$i]->id,$mossLisst[$i]->title) .'" class="news-style2">'.Str::limit($mossLisst[$i]->descriptions,Options::PAGE_HOME_CATEGORY_DESC_WITHOUT_IMG_CHAR).'</a>
					</div>
	    		';
	    		}else{
	    			$display = $display.'
	    					<div class="col-sm-12 col-md-12 col-lg-12 clear-padding" style="padding-left:15px;padding-right:15px;padding-top:20px;">
	    						<a href="'.SEO::generateNewsURL($mossLisst[$i]->id,$mossLisst[$i]->title) .'" class="news-style2">'.Str::limit($mossLisst[$i]->descriptions,Options::PAGE_HOME_CATEGORY_DESC_WITHOUT_IMG_CHAR).'</a>
	    					</div><div class="clearfix"></div>
	    			';
	    		}
	    		if(isset($mossLisst[$i+1])&& !empty($mossLisst[$i+1]->featurePhoto)) {
	    			$display=$display.'	    		
		    			<div class="col-sm-12 col-md-3 col-lg-3 clear-padding" style="padding-left:15px; padding-top:20px;">
		    				 <a href="'.SEO::generateNewsURL($mossLisst[$i+1]->id,$mossLisst[$i]->title) .'"><img src="../'.$mossLisst[$i+1]->featurePhoto.'" class="img-responsive" width="82" height="82"></a>
		    			 </div>
						<div class="col-sm-12 col-md-9 col-lg-9 clear-padding" style="padding-left:15px;padding-right:15px;padding-top:20px;">
							 <a href="'.SEO::generateNewsURL($mossLisst[$i+1]->id,$mossLisst[$i+1]->title) .'" class="news-style2">'.Str::limit($mossLisst[$i+1]->descriptions,Options::PAGE_HOME_CATEGORY_DESC_WITHOUT_IMG_CHAR).'</a>
						</div>
						
				    </div>
				   
		    		';
	    		}
    			if($m%3==0){ 	    		
	    			$display=$display."<div style='clear:both'></div>"; 
	    		}
	    		$m++;
	    		
	    	}
    		echo $display;
    	} 
    	
    	public function search() {  
    	
	    	$catList = Category::status(Options::STATUS_ACTIVE)->language()->orderBySequence("asc")->get(array(Options::DB_TABLE_CATEGORIES.'.id',Options::DB_TABLE_CATEGORIES.'.name'));		
			$mostViewed = News::status(Options::STATUS_ACTIVE)->orderByMostViewed('desc')->language()->take(Options::PAGE_HOME_MOST_VIEWED_LIMIT)->get(array(Options::DB_TABLE_NEWS.'.id', Options::DB_TABLE_NEWS.'.title', Options::DB_TABLE_CATEGORIES.'.name'));	    
			$mostCommented = News::status(Options::STATUS_ACTIVE)->orderByMostCommented('desc')->language()->take(Options::PAGE_HOME_MOST_COMMENTED_LIMIT)->get(array(Options::DB_TABLE_NEWS.'.id', Options::DB_TABLE_NEWS.'.title', Options::DB_TABLE_CATEGORIES.'.name'));
			
			foreach ($catList as $value )
				$catNews[$value->name] = News::status(Options::STATUS_ACTIVE)->orderByCreated('desc')->language()->take(6)
									->categoryID($value->id)->get(array(Options::DB_TABLE_NEWS.'.id', Options::DB_TABLE_NEWS.'.title', Options::DB_TABLE_NEWS.'.descriptions',Options::DB_TABLE_NEWS.'.featurePhoto', Options::DB_TABLE_CATEGORIES.'.name'));
			$headerMenu = 'home';
			//tong cac ban gi			
			$query = new News(); 
			
			if(isset($_REQUEST['keyword'])){
				$key = $_REQUEST['keyword'];
				$query = $query->where(Options::DB_TABLE_NEWS.'.title','LIKE','%'.$key.'%');
				$query = $query->leftJoin(Options::DB_TABLE_CATEGORIES, function($join) {
				      $join->on(Options::DB_TABLE_CATEGORIES.'.id', '=', Options::DB_TABLE_NEWS.'.categoryID');
				    })->leftJoin(Options::DB_TABLE_LANGUAGES, function($join) {
				      $join->on(Options::DB_TABLE_CATEGORIES.'.languageID', '=', Options::DB_TABLE_LANGUAGES.'.id');
				    })->where(Options::DB_TABLE_LANGUAGES.'.code', '=', Config::get('app.locale') );
			}else if(isset($_REQUEST['keyword']) && isset($_REQUEST['catId']) ){
				$key = $_REQUEST['keyword'];
				$cat_id = $_REQUEST['catId'];
				$query = $query->where(Options::DB_TABLE_NEWS.'.title','LIKE','%'.$key.'%');
				$query = $query->where(Options::DB_TABLE_NEWS.'.categoryID','=',$cat_id);
				$query = $query->leftJoin(Options::DB_TABLE_CATEGORIES, function($join) {
				      $join->on(Options::DB_TABLE_CATEGORIES.'.id', '=', Options::DB_TABLE_NEWS.'.categoryID');
				    })->leftJoin(Options::DB_TABLE_LANGUAGES, function($join) {
				      $join->on(Options::DB_TABLE_CATEGORIES.'.languageID', '=', Options::DB_TABLE_LANGUAGES.'.id');
				    })->where(Options::DB_TABLE_LANGUAGES.'.code', '=', Config::get('app.locale') );
			}
			$collection = $query->get();
			$count = $collection->count();			
			
			return View::make('search', array(
				'headerMenu' => $headerMenu, 
				'catList' => $catList,			
				'mostViewed' => $mostViewed, 
				'mostCommented' => $mostCommented, 
				'countItem'=>$count,				
			));
    		
    	}
    public function dataseach(){
   
    	if($_POST){
    		$page_number = filter_var($_POST["page_number"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);    		
    		$key = $_POST["key"];
    		$cat_id = $_POST["cat_id"];
    		$lag = $_POST['lag'];
    		$display="";    	
    		$position = ($page_number * 5);
    		$query = new News(); 
    		if($key!="" && $cat_id!=0){
    			$query = $query->where(Options::DB_TABLE_NEWS.'.title','LIKE','%'.$key.'%');
				$query = $query->where(Options::DB_TABLE_NEWS.'.categoryID','=',$cat_id);
				$query = $query->leftJoin(Options::DB_TABLE_CATEGORIES, function($join) {
				      $join->on(Options::DB_TABLE_CATEGORIES.'.id', '=', Options::DB_TABLE_NEWS.'.categoryID');
				    })->leftJoin(Options::DB_TABLE_LANGUAGES, function($join) {
				      $join->on(Options::DB_TABLE_CATEGORIES.'.languageID', '=', Options::DB_TABLE_LANGUAGES.'.id');
				    })->where(Options::DB_TABLE_LANGUAGES.'.code', '=', $lag );
    			
    		}else{
    			$query = $query->where(Options::DB_TABLE_NEWS.'.title','LIKE','%'.$key.'%');
				$query = $query->leftJoin(Options::DB_TABLE_CATEGORIES, function($join) {
				      $join->on(Options::DB_TABLE_CATEGORIES.'.id', '=', Options::DB_TABLE_NEWS.'.categoryID');
				    })->leftJoin(Options::DB_TABLE_LANGUAGES, function($join) {
				      $join->on(Options::DB_TABLE_CATEGORIES.'.languageID', '=', Options::DB_TABLE_LANGUAGES.'.id');
				    })->where(Options::DB_TABLE_LANGUAGES.'.code', '=', $lag );
    		}
    		$query = $query->offset($position);
    		$query = $query->take(5);	
			$datas = $query->get(array(Options::DB_TABLE_NEWS.'.id',Options::DB_TABLE_NEWS.'.title',Options::DB_TABLE_NEWS.'.descriptions',Options::DB_TABLE_NEWS.'.created_at'));
			
			foreach ($datas as $data){
				$display=$display.'
		    		<div class="item col-md-12 col-lg-12 col-xs-12 col-sm-12 clear-padding nation" >
				   		<h5 class="title5"><a href="'.SEO::generateNewsURL($data->id,$data->title).'">'.$data->title.'</a></h5>
						<div class="time-date">'. date(Options::DATE_TIME_FORMAT,strtotime($data->created_at)) .'</div>
						<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 clear-padding">'.Str::limit($data->descriptions, Options::PAGE_HOME_LATEST_NEWS_DESC_WITH_IMG_CHAR).'</div>
						<div class="clearfix"></div>
						<hr>
					</div>
		    	';
    	
			}
			echo $display;
    	}
    	
    	
    }

  
	
}
